package com.zhengtoudai.base;

import android.app.Activity;
import android.content.Context;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.squareup.leakcanary.LeakCanary;
import com.squareup.otto.Bus;
import com.tencent.bugly.crashreport.CrashReport;
import com.zhengtoudai.injector.component.AppComponent;
import com.zhengtoudai.injector.component.DaggerAppComponent;
import com.zhengtoudai.injector.module.AppModule;
import com.zhengtoudai.otto.LoginOutEvent;
import com.zhengtoudai.util.Session;
import com.zhengtoudai.util.SharedPreferencesSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;


/**
 * 作者：黑哥 on 2016/3/29 10:09
 */
public class App extends MultiDexApplication {
    private AppComponent mAppComponent;

    private String TAG = this.getClass().getSimpleName();

    public static App mApp;
    //存储数据

    Session session;
    //activity的集合用于添加或关闭activity
    public List<Activity> mAcitivity;

    @Inject
    Bus mBus;
    @Override
    public void onCreate() {
        super.onCreate();
        initComponent();
        mApp = this;
        this.session = new SharedPreferencesSession(this);
        /**bug检测初始化*/
        CrashReport.initCrashReport(getApplicationContext(), "8d1af10ba0", false);
        //检查内存泄漏
        LeakCanary.install(this);
    }

    private void initComponent() {

        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
        mAppComponent.inject(this);
    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }

    /**
     * 添加activity
     */
    public void addActivity(Activity activity) {
        if (mAcitivity == null) {
            mAcitivity = new ArrayList<Activity>();
        }
        mAcitivity.add(activity);
    }

    /**
     * 移除activity
     */
    public void removeActivity() {
        if (mAcitivity != null && mAcitivity.size() > 0) {
            for (int i = 0; i < mAcitivity.size(); i++) {
                mAcitivity.get(i).finish();
            }
            mAcitivity.clear();
        }
    }

    //得到
    public Session getSession() {
        return session;
    }

    //设置
    public void setSession(Session session) {
        this.session = session;
    }

    // 存储数据比较
    public boolean sessionEqual(String key, String cVal) {
        return this.session.get(key) != null
                && this.session.get(key).equals(cVal);
    }


    public boolean hasLogin() {
        if (!TextUtils.isEmpty(getSession().get("JSESSIONID")) &&
                !TextUtils.isEmpty(getSession().get("token"))) {
            return true;
        } else {
            return false;
        }
    }

    // 退出清空session
    public void logout() {
        mBus.register(this);
        mBus.post(new LoginOutEvent());
        mBus.unregister(this);
        Map<String, String> vals = new HashMap<String, String>();
        vals.put("JSESSIONID", "");
        vals.put("token", "");
        this.session.set(vals);
    }


    // 依据key value 存储数据
    public void setSessionVal(String key, String value) {
        this.session.set(key, value);
    }


    public static Context getContext() {
        return mApp.getApplicationContext();
    }
}
